﻿using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.Context;

namespace ufida.u9.cust.dz.beplugin.MiscRcvTrans
{
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using ufida.u9.cust.dz.beplugin.JkySchema;
    using ufida.u9.cust.dz.beplugin.loger;
    using ufida.u9.cust.dz.beplugin.Utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.InvDoc.Enums;
    using UFIDA.U9.InvDoc.MiscRcv;

    class AfterUpdated : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (PlatformContext.Current.EnterpriseID.Equals("10"))
            {
                MiscRcvTrans entity = null;
                if (BaseCommon.TryGetEntity(args, ref entity))
                {
                    INVDocStatus Current = entity.Status;
                    INVDocStatus Original = entity.OriginalData.Status;

                    if (Current == INVDocStatus.Approved && Original == INVDocStatus.Approving)
                    {

                        JkyHead obj = TurnParams(entity);
                        string jsonObj = JsonConvert.SerializeObject(obj);
                        WebResult res = Task.Run(() => JkyUtils.Post("erp.stock.createandstockin", jsonObj)).GetAwaiter().GetResult();
                        LogUtil logUtil = new LogUtil();
                        if (res.Sucess)
                        {
                            JkyResponse jkyResponse = JsonConvert.DeserializeObject<JkyResponse>(res.Data);
                            if (!jkyResponse.code.Equals("200"))
                            {
                                logUtil.RecordLog(entity.DocNo, "杂收", obj, 0, jkyResponse.msg);
                                throw new Exception("推送吉克云失败:" + jkyResponse.msg);
                            }
                            else
                            {
                                logUtil.RecordLog(entity.DocNo, "杂收", obj, 1, "推送成功");
                            }
                        }
                        else
                        {
                            logUtil.RecordLog(entity.DocNo, "杂收", obj, 0, JsonUtils.Serialize(res));
                            throw new Exception("推送失败");
                        }

                    }
                }
            }
        }

        private JkyHead TurnParams(MiscRcvTrans entity)
        {
            JkyHead head = new JkyHead();

            head.applyDate = entity.BusinessDate.ToString("yyyy-MM-dd HH:mm:ss");
            head.applyCompanyCode = "00" + entity.Org.Code;
            head.relDataId = entity.DocNo;
            head.applyDepartCode = entity.WhMan.Dept.Code + "-" + entity.Org.Code;
            head.inWarehouseCode = entity.MiscRcvTransLs[0].Wh.Code;
            head.applyUserName = entity.WhMan.Name;
            head.applyDepartName = entity.WhMan.Dept.Name;
            head.@operator = Context.LoginUser;
            head.auditDate = entity.BusinessDate.ToString("yyyy-MM-dd HH:mm:ss");
            head.planInDate = entity.BusinessDate.ToString("yyyy-MM-dd HH:mm:ss");
            head.stockInDetailViews = new List<JkyLine>();

            foreach (var line in entity.MiscRcvTransLs)
            {
                JkyLine lineObj = new JkyLine();

                lineObj.outSkuCode = line.ItemInfo.ItemCode;
                lineObj.relDetailId = line.ID.ToString();
                lineObj.skuCount = line.StoreUOMQty;
                //批号
                if (line.LotInfo != null && line.LotInfo.LotMaster != null)
                {
                    lineObj.batchList = new List<Batch>() { new Batch() { batchNo = line.LotInfo.LotCode, quantity = line.StoreUOMQty, productionDate = line.LotInfo.LotMaster.EffectiveDatetime.ToString("yyyy-MM-dd HH:mm:ss") } };
                }

                head.stockInDetailViews.Add(lineObj);
            }

            return head;
        }
    }
}
